import java.io.*;
import java.rmi.*;
import java.util.*;

class Client 
{
   public static void main(String [] args) throws Exception
   {String []adress={"localhost:9001","localhost:9002","localhost:9003","localhost:9004"};
 if(args.length!=3 || args.length==0)
System.out.println("USAGE: java Client [MB.txt] [MC.txt] [MZ.txt]");
 else{	   
   new Client(args[0], args[1], args[2], adress);  
 }
   }
  final static int HOST_COUNT=4;

  private double matrixA [][];
  private double matrixA1[][]; private double matrixA2[][];
  private double matrixA3[][]; private double matrixA4[][];
  
  private double matrixB [][];
  private double matrixC [][];

  private double matrixCZ[][];
  private double matrixCZ1[][]; private double matrixCZ2[][];
  private double matrixCZ3[][]; private double matrixCZ4[][];
  
  private double matrixZ[][];
  private double matrixZ1[][]; private double matrixZ2[][];
  private double matrixZ3[][]; private double matrixZ4[][];
  
  private int d1,d2,d3,d4;
	  
  Client(String f1, String f2, String f3, String [] adress) throws Exception
  {
  System.setProperty("java.security.policy","client.policy");
  System.setSecurityManager(new RMISecurityManager());
  matrixB = new FileRead(f1).getMatrix();
  matrixC = new FileRead(f2).getMatrix();
  matrixZ = new FileRead(f3).getMatrix();
  
if(matrixB[0].length!=matrixC.length||matrixC[0].length!=matrixZ.length)
  { 
   System.out.println("Nevirnyy rozmir vyhidnyh danyh !!!\n");  
   System.exit(0); 
  }
  matrixA = new double[matrixB.length][matrixZ[0].length];
  
  if(matrixZ[0].length<HOST_COUNT)
   {
   new NoName(matrixB,matrixC,matrixZ,adress);
   }
  else
  {
  if(matrixZ[0].length%HOST_COUNT!=0)
{
int z=matrixZ[0].length%HOST_COUNT;int d=matrixZ[0].length/HOST_COUNT;
if(z==2){d1=d+1;d2=d+1;d3=d;d4=d;}
else{if(z==3){d1=d+1;d2=d+1;d3=d+1;d4=d;}else{d1=d+1;d2=d;d3=d;d4=d;}}
    }
      else
          { d1=d2=d3=d4=matrixZ[0].length/HOST_COUNT;  }
  }
  
  matrixZ1 = new double[matrixZ.length][d1]; 
  matrixZ2 = new double[matrixZ.length][d2]; 
  matrixZ3 = new double[matrixZ.length][d3]; 
  matrixZ4 = new double[matrixZ.length][d4]; 
 
  matrixCZ1= new double[matrixC.length][d1];
  matrixCZ2= new double[matrixC.length][d2];
  matrixCZ3= new double[matrixC.length][d3];
  matrixCZ4= new double[matrixC.length][d4];
  
  matrixA1 = new double[matrixB.length][d1]; 
  matrixA2 = new double[matrixB.length][d2]; 
  matrixA3 = new double[matrixB.length][d3]; 
  matrixA4 = new double[matrixB.length][d4]; 

for(int i=0;i<matrixZ.length;i++)
  for(int j=0;j<d1;j++) 
  matrixZ1[i][j] = matrixZ[i][j];
for(int i=0;i<matrixZ.length;i++)
  for(int k=d1,j=0;k<(d2+d1);k++,j++) 
  matrixZ2[i][j] = matrixZ[i][k]; 
for(int i=0;i<matrixZ.length;i++)
  for(int k=d2+d1,j=0;k<d3+d2+d1;k++,j++) 
  matrixZ3[i][j] = matrixZ[i][k]; 
for(int i=0;i<matrixZ.length;i++)
  for(int k=d3+d2+d1,j=0;k<d4+d3+d2+d1;k++,j++) 
  matrixZ4[i][j] = matrixZ[i][k]; 

try{
O [] host= new O[HOST_COUNT];
for(int i=0;i<HOST_COUNT;i++)
host[i] = (O)Naming.lookup("rmi://"+adress[i]+"/OServer");   

matrixCZ1=host[0].getMatrix(matrixC,matrixZ1);
matrixCZ2=host[1].getMatrix(matrixC,matrixZ2);
matrixCZ3=host[2].getMatrix(matrixC,matrixZ3);
matrixCZ4=host[3].getMatrix(matrixC,matrixZ4);

matrixA1=host[0].getMatrix(matrixB,matrixCZ1);
matrixA2=host[1].getMatrix(matrixB,matrixCZ2);
matrixA3=host[2].getMatrix(matrixB,matrixCZ3);
matrixA4=host[3].getMatrix(matrixB,matrixCZ4);

}catch(Exception e){System.out.println("RMI Exception: "+e);}

for(int i=0;i<matrixA.length;i++)
  for(int j=0;j<d1;j++) 
  matrixA[i][j] = matrixA1[i][j];
for(int i=0;i<matrixA.length;i++)
  for(int k=d1,j=0;k<(d2+d1);k++,j++) 
  matrixA[i][k] = matrixA2[i][j]; 
for(int i=0;i<matrixA.length;i++)
  for(int k=d2+d1,j=0;k<d3+d2+d1;k++,j++) 
  matrixA[i][k] = matrixA3[i][j]; 
for(int i=0;i<matrixA.length;i++)
  for(int k=d3+d2+d1,j=0;k<d4+d3+d2+d1;k++,j++) 
  matrixA[i][k] = matrixA4[i][j]; 

  FileWriter fw = new FileWriter("result.txt",false);
for(int i=0;i<matrixA.length;i++)
{  for(int j=0;j<matrixA[0].length;j++)
  fw.write(matrixA[i][j]+" ");
  fw.write("\n");
}
  fw.close();
  }
}
